/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.view.components.extended
{
	import multimedia.control.event.media.DeleteMediaEvent;
	import multimedia.model.vo.MediaVO;
	
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.Tree;
	import mx.core.DragSource;
	import mx.events.CloseEvent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;

	public class TrashCan extends Image
	{
		public function TrashCan()
		{
			super();
			addEventListener(DragEvent.DRAG_ENTER, onDragEnter);
			addEventListener(DragEvent.DRAG_OVER, onDragOver);
			addEventListener(DragEvent.DRAG_DROP, onDragDrop);
			addEventListener(DragEvent.DRAG_EXIT, onDragExit);
			addEventListener(DragEvent.DRAG_COMPLETE, onDragComplete);
		}
		
		private function onDragEnter(event:DragEvent):void
		{
			var target:Image = event.target as Image;
			DragManager.acceptDragDrop(target);
		}

		private function onDragOver(event:DragEvent):void
		{
			var target:Image = event.target as Image;
			event.preventDefault();
		}

		private function onDragDrop(event:DragEvent):void
		{
			var target:Image = event.target as Image;
			event.preventDefault();
			(event.dragInitiator as Tree).hideDropFeedback(event);
			var ds:DragSource = event.dragSource;
			var arr:Array;
			if (ds.hasFormat("treeItems"))
			{
				arr = ds.dataForFormat("treeItems") as Array;
			}
			Alert.show("Möchten Sie dieses Medium wirklich löschen? Die Aktion kann nicht rückgängig gemacht werden.", 
					"Medium löschen", Alert.YES | Alert.NO, this, 
					function handler(event:CloseEvent):void
					{
						if (event.detail == Alert.YES)
						{
							new DeleteMediaEvent(arr[0] as MediaVO).dispatch();
						}
					});
		}

		private function onDragExit(event:DragEvent):void
		{
			(event.dragInitiator as Tree).hideDropFeedback(event);
		}

		private function onDragComplete(event:DragEvent):void
		{
			(event.dragInitiator as Tree).hideDropFeedback(event);
		}
		
	}
}